package com.sq.shortlink.admin.remoto;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sq.shortlink.admin.common.convension.result.Result;
import com.sq.shortlink.admin.remoto.dto.req.ShortLinkCreateReqDTO;
import com.sq.shortlink.admin.remoto.dto.req.ShortLinkPageReqDTO;
import com.sq.shortlink.admin.remoto.dto.req.ShortLinkStatsAccessRecordReqDTO;
import com.sq.shortlink.admin.remoto.dto.resp.ShortLinkCreateRespDTO;
import com.sq.shortlink.admin.remoto.dto.resp.ShortLinkGroupQueryCountRespDTO;
import com.sq.shortlink.admin.remoto.dto.resp.ShortLinkPageRespDTO;
import com.sq.shortlink.admin.remoto.dto.resp.ShortLinkStatsAccessRecordRespDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 * @author quan
 */
public interface ShortLinkRemoteService {
    /**
     * 创建短链接
     * @param requestParam 创建短链接请求参数
     * @return 创建短链接响应参数
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://localhost:8001/api/short-link/v1/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr, new TypeReference<>() {
        });
    }

    /**
     * 短链接分页查询
     * @param requestParam 短链接分页查询请求参数
     * @return 短链接分页查询响应参数
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        Map<String,Object> map = new HashMap<>();
        map.put("gid",requestParam.getGid());
        map.put("current",requestParam.getCurrent());
        map.put("size",requestParam.getSize());
        String resultBodyStr = HttpUtil.post("http://localhost:8001/api/short-link/v1/page", JSON.toJSONString(map));
        return JSON.parseObject(resultBodyStr, new TypeReference<>() {
        });
    }

    /**
     * 短链接组内短链接数量查询
     * @param requestParam 短链接标识集合
     * @return 短链接数量集合
     */
    default Result<List<ShortLinkGroupQueryCountRespDTO>> listGroupShortLinkCount(List<String> requestParam){
        Map<String,Object> map = new HashMap<>();
        map.put("requestParam",requestParam);
        String resultBodyStr = HttpUtil.get("http://localhost:8001/api/short-link/v1/count", map);
        return JSON.parseObject(resultBodyStr, new TypeReference<>() {
        });
    }

    /**
     * 获取网站标题通过URL
     * @param url 网站链接
     * @return 网站标题
     */
    default Result<String> getTitleByUrl(String url){
        Map<String,Object> map = new HashMap<>();
        map.put("url",url);
        String resultStr = HttpUtil.get("http://localhost:8001/api/short-link/v1/title", map);
        return JSON.parseObject(resultStr, new TypeReference<Result<String>>() {
        });
    }

    /**
     * 短链接监控访问日志记录分页查询
     * @param requestParam 分页查询请求参数
     * @return 分页查询结果集
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> listShortLinkStatsAccessRecord(ShortLinkStatsAccessRecordReqDTO requestParam){
        Map<String, Object> map = BeanUtil.beanToMap(requestParam, false, true);
        map.remove("orders");
        map.remove("records");
        String resultStr = HttpUtil.get("http://localhost:8001/api/short-link/v1/stats/access-record", map);
        return JSON.parseObject(resultStr, new TypeReference<>() {});
    }
}
